﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WeChatSDK.Core.Models;

namespace WeChatSDK.Core
{
    public class ReceiveMessage
    {
        /// <summary>
        /// 返回根据微信 生成的签名
        /// </summary>
        public string GenSignature(string token, string timestamp, string nonce)
        {
            List<string> sortArray = new List<string>();
            //sortArray.Add(signature);
            sortArray.Add(token);
            sortArray.Add(timestamp);
            sortArray.Add(nonce);
            //sortArray.Add(echostr);

            sortArray = sortArray.OrderBy(m => m).ToList(); //微信微信需要的排序方式重新排序
            StringBuilder sb = new StringBuilder();
            string realsignature = string.Empty;
            foreach (string item in sortArray) //编码
            {
                sb.Append(item);
            }
            realsignature = ""; //sha1编码

            return realsignature;
        }
        /// <summary>
        /// 判断收的信息的真伪
        /// </summary>
        public bool CheckSignature(string token, string timestamp, string nonce, string signature, string echostr = "")
        {
            string realsignature = GenSignature(token, timestamp, nonce);

            if (realsignature == signature) //重新匹配签名
                return true;
            else
                return false;
        }
        /// <summary>
        /// 用户发送到微信服务号的信息
        /// </summary>
        public UserMessageModel UserMessage(string message_content)
        {
            UserMessageModel model = new UserMessageModel();
            return model;
        }

    }
}
